********************************************************************************
clear all
********************************************************************************


//Load data & merge data
////////////////////////////////////////////////////////////////////////////////


***TEMPLATE A***																data_101-107.dta
use data_101-107_179-199.dta
replace arrondissement = "99" if arrondissement=="Huy"
destring arrondissement, replace
des	


	//String/numeric variables
tostring concession_date, replace
tostring k_*_n, replace

tempfile template_a
save `template_a'


***TEMPLATE B***																data_109-113.dta/data_114-118.dta
use data_114-118.dta, clear
append using data_108-113_200-204.dta
sort series year arrondissement ordernr
des

	//String/numeric variables
tostring concession_date, replace
tostring k_*_n, replace


tempfile template_b
save `template_b'


***TEMPLATE C***																data_118-124.dta
use data_118-124.dta, clear
drop mine_id mine_subid_* mine_merger*

tempfile template_c
save `template_c'

***MERGE***
use `template_a', clear
append using `template_b'
append using `template_c'


//Organize variables
////////////////////////////////////////////////////////////////////////////////

/*
global var_A "series year district arrondissement ordernr mapnr mine_name mine_community concession_date concession_ha concession_ha_prov operations_active operations_reserve operations_construction pit_name pit_power vein_n avgvein_thickness"
global var_B1 "k_extraction_v_n k_extraction_m_n k_extraction_t_n k_extraction_hp k_extraction_hp2 k_extraction_hp_m k_extraction_hn"
global var_B2 "k_excavation_t_n k_excavation_p_n k_excavation_pp_n k_excavation_n k_excavation_hp k_excavation_hn k_ventilation k_ventilation_n k_ventilation_hp k_other_n k_other_hp"
global var_C "galleries_transport_n galleries_water_n horses_underground_n horses_surface_n"
global var_D11 "l_under_men_n l_under_men_w_org l_under_men_w l_under_women_n l_under_women_w_org l_under_women_w l_under_boys_n l_under_boys_w_org l_under_boys_w l_under_girls_n l_under_girls_w_org l_under_girls_w"
global var_D12 "l_under_total_n l_under_total_w l_under_total_w_org l_under_total_w_qb l_under_total_w_ab l_under_total_w_an"
global var_D21 "l_surface_men_n l_surface_men_w_org l_surface_men_w l_surface_women_n l_surface_women_w_org l_surface_women_w l_surface_boys_n l_surface_boys_w_org l_surface_boys_w l_surface_girls_n l_surface_girls_w_org l_surface_girls_w"
global var_D22 "l_surface_total_n l_surface_total_w_org l_surface_total_w  l_surface_total_w_qb l_surface_total_w_ab l_surface_total_w_an" 
global var_E "l_total_n l_total_w  l_total_w_org l_total_w_qb l_total_w_ab l_total_w_an"
global var_F1 "q_leans_gr_t q_leans_gr_p q_leans_glx_t q_leans_glx_p q_leans_mn_t q_leans_mn_p q_leans_tot_t q_drys_gr_t q_drys_gr_p q_drys_glx_t q_drys_glx_p q_drys_tot_t q_leanl_gr_t q_leanl_gr_p q_leanl_gal_t q_leanl_gal_p q_leanl_glx_t q_leanl_glx_p q_leanl_tot_t q_fatl_gr_t q_fatl_gr_p q_fatl_glx_p q_fatl_glx_q q_fatl_tot_t q_fatty_gr_t q_fatty_gr_p q_fatty_glx_t q_fatty_glx_p q_fatty_tot_t q_total_gr_t q_total_gal_t q_total_glx_t q_total_mn_t"
global var_F2 "q_dry_t q_dry_p q_leans_t q_leans_p q_leanl_t q_leanl_p q_halfl_t q_halfl_p q_fatty_t q_fatty_p"
global var_F3 "q_total_t q_total_pq q_total_p"
global var_G "expenses_l expenses_other expenses_total balance_gain balance_loss expenses_prep expenses_std_l expenses_std_other expenses_std_total prixderevient_std_t expenses_spc_l expenses_spc_other expenses_spc_total prixderevient_t"
global var_E "observations consumption_t consumption_pq consumption_stock"
*/

//Identify mines
////////////////////////////////////////////////////////////////////////////////


*sort mine_name year mine_community concession_ha
drop if missing(mine_name)
drop if mine_name == "missing?"

***************
run DO_Mine_id.do
***************



//Put manually assigned ID's into numeric ID variable
encode mine_id, gen(id)

//Load numeric ID's for submines
preserve 
keep mine_id id
duplicates drop
foreach k of numlist 1/5 {
gen mine_subid_`k' = mine_id
gen id_sub`k' = id
}
tempfile idlist
save `idlist'
restore

foreach k of numlist 1/5 {
merge m:1 mine_subid_`k' using `idlist', keepusing(id_sub`k')
drop if _merge==2
drop _merge
}





//Load extra information on mines using manual (string) ID variable
////////////////////////////////////////////////////////////////////////////////

sort mine_id year
save data_adm.dta, replace

//Coverage:
tab year
